package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.ServiceException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.rowset.serial.SerialException;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    //注入Dish和Setmeal业务对象
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;


    @Override
    public boolean remove(Long id) {
        LambdaQueryWrapper<Category> queryWrapper_cate=new LambdaQueryWrapper<>();
        queryWrapper_cate.eq(Category::getId,id);
        //查看当前分类是否包含菜品
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,id);
        int count = dishService.count(queryWrapper);
        if (count>0){
            //表示该分类包含菜品，抛出一个异常，给用户提示信息
//            throw new ServiceException("该分类中包含菜品，不能删除");
            return false;
        }
        LambdaQueryWrapper<Setmeal> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(queryWrapper1);
        if (count1>0){
            //表示该分类包含套餐，抛出一个异常，给用户提示信息
//            throw new ServiceException("该分类中包含了套餐，不能删除");
            return false;
        }

        //要是上面两种情况都没有就表明分类中不包含菜品或者套餐，可以删除
        //调用福诶ServiceImpl的remove方法
        boolean remove = super.remove(queryWrapper_cate);
        return remove;
    }
}
